package com.gmall.product.controller;

import com.aliyun.oss.OSS;
import com.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("admin/product/fileUpload")
@CrossOrigin("*")//http://localhost:8888
public class FileUploadController {


    @PostMapping("/native")
    public Result<String> uploadNative(MultipartFile file) {
        return Result.ok("文件上传成功");
    }

    @PostMapping("/fastDFS")
    public Result<String> uploadFastDFS(MultipartFile file) {
        return Result.ok("文件上传成功");
    }

    /**
     * 第一种方式：
     * 1、pom中依赖  spring-cloud-alicloud-oss
     *       <dependency>
    *             <groupId>com.alibaba.cloud</groupId>
    *             <artifactId>spring-cloud-alicloud-oss</artifactId>
    *             <version>2.1.0.RELEASE</version>
    *         </dependency>
     * 2、需要在配置文件中配置
     *  spring:
     *   cloud:
     *     alicloud:
     *       access-key: QwGbUg5VOOk5kttE
     *       secret-key: o49ZIyI02MoBwjU4yEwBrLfDg0RARG
     *       oss:
     *         endpoint: oss-cn-beijing.aliyuncs.com
     *  3、编写代码
     *  @Autowired
     *  OSS ossClient;
     *
     *  第二种方式：
     *  1、pom中依赖  aliyun-sdk-oss
     *          <dependency>
     *             <groupId>com.aliyun.oss</groupId>
     *             <artifactId>aliyun-sdk-oss</artifactId>
     *             <version>3.10.2</version>
     *         </dependency>
     *  2、自己写配置OssConfig，详见代码
     *      注意 配置信息的对应
     *  3、编写代码
     *     @Autowired
     *     OSS ossClient;
     */
    @Autowired //byType
    OSS ossClient;

    // 桶名称
    //@Value("${aliyun.oss.bucketName}")
    //private String bucketName;
    String bucketName = "yjs-2203a";
    @PostMapping("/oss")
    public Result<String> uploadOss(MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String contentType = file.getContentType();
        String objectName = originalFilename;
        ossClient.putObject(bucketName, objectName, file.getInputStream());

        //https://yjs-2203a.oss-cn-beijing.aliyuncs.com/zhuanggao5/%E8%81%94%E6%83%B3.png
        //url= https://yjs-2203a.oss-cn-beijing.aliyuncs.com/xiaomi.png
        String url = "https://" + bucketName + ".oss-cn-beijing.aliyuncs.com/" + objectName;
        System.out.println("url = " + url);
        return Result.ok(url);
    }

    @PostMapping("/oss/multi")
    public Result<List<Map>> uploadOssMulti(MultipartFile[] file) throws IOException {

        List<Map> imageUrlList = Arrays.stream(file).map(
                (file1) -> {
                    String originalFilename = file1.getOriginalFilename();
                    String contentType = file1.getContentType();
                    String objectName = originalFilename;
                    try {
                        ossClient.putObject(bucketName, objectName, file1.getInputStream());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    //https://yjs-2203a.oss-cn-beijing.aliyuncs.com/zhuanggao5/%E8%81%94%E6%83%B3.png
                    //url= https://yjs-2203a.oss-cn-beijing.aliyuncs.com/xiaomi.png
                    String url = "https://" + bucketName + ".oss-cn-beijing.aliyuncs.com/" + objectName;
                    System.out.println("url = " + url);
                    Map<String,String> map = new HashMap<>();
                    map.put("url",url);
                    map.put("name",originalFilename);
                    return map;
                }
        ).collect(Collectors.toList());

        return Result.ok(imageUrlList);
    }
}
